# -*- python -*-
# ex: set syntax=python:

# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

from buildbot.scheduler import Scheduler
from buildbot.schedulers import timed
from buildbot.status import mail

from common import chromium_utils

from master import gitiles_poller
from master import master_utils
from master import slaves_list
from master.factory import annotator_factory

import config
import master_site_config

ActiveMaster = master_site_config.ChromiumSwarm

# This is the dictionary that the buildmaster pays attention to. We also use
# a shorter alias to save typing.
c = BuildmasterConfig = {}

# 'slavePortnum' defines the TCP port to listen on. This must match the value
# configured into the buildslaves (with their --master option)
c['slavePortnum'] = ActiveMaster.slave_port

config.DatabaseSetup(c, require_dbconfig=ActiveMaster.is_production_host)

####### CHANGESOURCES

# Polls config.Master.trunk_url for changes
master_poller = gitiles_poller.GitilesPoller(
    'https://chromium.googlesource.com/chromium/src')

c['change_source'] = [master_poller]

####### SCHEDULERS

s_chromium_swarm = Scheduler(name='experimental',
                             branch='master',
                             treeStableTimer=60,
                             builderNames=['Linux Swarm Tests',
                                           'Linux Swarm Tests (dbg)',
                                           'Windows Swarm Tests',
                                           'Windows Swarm Tests (dbg)',
                                           'Mac Swarm Tests',
                                           'Mac Swarm Tests (dbg)',
                                           'Linux deterministic build',
                                           'Windows deterministic build',
                                           'Windows Clang deterministic build',
                                           'Mac deterministic build',
                                           'Android deterministic build'])

s_heartbeat = timed.Periodic(
    name='heartbeat', builderNames=['Heartbeat', 'Heartbeat Canary'],
    periodicBuildTimer=60)

c['schedulers'] = [s_chromium_swarm, s_heartbeat]


# ----------------------------------------------------------------------------
# BUILDER DEFINITIONS

def RecipeBasedFactory(config):
  return annotator_factory.AnnotatorFactory().BaseFactory(
      'swarming/canary', factory_properties={'configuration': config})

def DeterministicBuilderFactory(platform):
  return  {
      'name': '%s deterministic build' % platform,
      # Work around an issue with NaCl build.
      'slavebuilddir': 'chr',
      'factory': annotator_factory.AnnotatorFactory().BaseFactory(
          'swarming/deterministic_build',
          factory_properties = {
            'configuration': 'Release',
          }),
      # Auto-reboot:
      # - On windows to try to clear up TEMP fill up issues.
      # - On OSX due to fork: Resource temporarily unavailable
      'auto_reboot': True,
      'category': 'deterministic',
    }

b_heartbeat = {
  'name': 'Heartbeat',
  'builddir': 'heartbeat',
  'factory': annotator_factory.AnnotatorFactory().BaseFactory(
      'swarming/heartbeat'),
  'auto_reboot': False,
  'category': 'swarming',
}

b_heartbeat_canary = {
  'name': 'Heartbeat Canary',
  'builddir': 'heartbeat_canary',
  'factory': annotator_factory.AnnotatorFactory().BaseFactory(
      'swarming/heartbeat',
      factory_properties={'target_environment': 'canary'}),
  'auto_reboot': False,
  'category': 'swarming',
}

b_chromium_linux_swarm = {
  'name': 'Linux Swarm Tests',
  'builddir': 'chromium-linux-swarm',
  'factory': RecipeBasedFactory('Release'),
  'auto_reboot': False,
  'category': 'swarming',
}

b_chromium_linux_swarm_dbg = {
  'name': 'Linux Swarm Tests (dbg)',
  'builddir': 'chromium-linux-swarm-dbg',
  'factory': RecipeBasedFactory('Debug'),
  'category': 'swarming',
}

b_chromium_win_swarm = {
  'name': 'Windows Swarm Tests',
  'builddir': 'chromium-win-swarm',
  'factory': RecipeBasedFactory('Release'),
  # Auto-reboot on windows to try to clear up TEMP fill up issues.
  'auto_reboot': True,
  'category': 'swarming',
}

b_chromium_win_swarm_dbg = {
  'name': 'Windows Swarm Tests (dbg)',
  'builddir': 'chromium-win-swarm-dbg',
  'factory': RecipeBasedFactory('Debug'),
  # Auto-reboot on windows to try to clear up TEMP fill up issues.
  'auto_reboot': True,
  'category': 'swarming',
}

b_chromium_mac_swarm = {
  'name': 'Mac Swarm Tests',
  'builddir': 'chromium-mac-swarm',
  'factory': RecipeBasedFactory('Release'),
  'auto_reboot': False,
  'category': 'swarming',
}

b_chromium_mac_swarm_dbg = {
  'name': 'Mac Swarm Tests (dbg)',
  'builddir': 'chromium-mac-swarm-dbg',
  'factory': RecipeBasedFactory('Debug'),
  'auto_reboot': False,
  'category': 'swarming',
}

b_win_deterministic_build = DeterministicBuilderFactory('Windows')
b_win_clang_deterministic_build = DeterministicBuilderFactory('Windows Clang')
b_linux_deterministic_build = DeterministicBuilderFactory('Linux')
b_mac_deterministic_build = DeterministicBuilderFactory('Mac')
b_android_deterministic_build = DeterministicBuilderFactory('Android')

c['builders'] = [
  b_heartbeat,
  b_heartbeat_canary,
  b_chromium_linux_swarm,
  b_chromium_linux_swarm_dbg,
  b_chromium_win_swarm,
  b_chromium_win_swarm_dbg,
  b_chromium_mac_swarm,
  b_chromium_mac_swarm_dbg,
  b_win_deterministic_build,
  b_win_clang_deterministic_build,
  b_linux_deterministic_build,
  b_mac_deterministic_build,
  b_android_deterministic_build,
]

# Associate the slaves to the builders. The configuration is in slaves.cfg.
slaves = slaves_list.SlavesList('slaves.cfg', 'ChromiumSwarm')
for builder in c['builders']:
  builder['slavenames'] = slaves.GetSlavesName(builder=builder['name'])


####### BUILDSLAVES

# The 'slaves' list defines the set of allowable buildslaves. List all the
# slaves registered to a builder. Remove dupes.
c['slaves'] = master_utils.AutoSetupSlaves(c['builders'],
                                           config.Master.GetBotPassword())

# Make sure everything works together.
master_utils.VerifySetup(c, slaves)


####### STATUS TARGETS

# Must come before AutoSetupMaster().
c['buildbotURL'] = ActiveMaster.buildbot_url

# Adds common status and tools to this master.
master_utils.AutoSetupMaster(c, ActiveMaster,
    public_html="../master.chromium/public_html",
    templates=['../master.chromium/templates'],
    tagComparator=master_poller.comparator,
    enable_http_status_push=ActiveMaster.is_production_host)

c['status'].append(mail.MailNotifier(
    fromaddr=ActiveMaster.from_address,
    builders=['Heartbeat'],
    subject='Swarming heartbeat failed',
    mode='failing',
    relayhost=config.Master.smtp,
    sendToInterestedUsers=False,
    extraRecipients=[
      'swarming-eng+alert@google.com',
    ]))

c['status'].append(mail.MailNotifier(
    fromaddr=ActiveMaster.from_address,
    builders=['Heartbeat'],
    subject='Swarming heartbeat failed',
    mode='problem',
    relayhost=config.Master.smtp,
    sendToInterestedUsers=False,
    extraRecipients=[
      'chrome-troopers+swarming@google.com',
    ]))


####### PROJECT IDENTITY

c['projectName'] = ActiveMaster.project_name
c['projectURL'] = config.Master.project_url
